use futures_util::Future;
use tokio::time::{ self, Duration };

pub async fn interval<T>(secs:u64, closure:T) where T:Fn(){
    let mut interval = time::interval(Duration::from_secs(secs));
    // 表示如果错过计时间隔，则会立即执行多个周期，直到被重新赶上
    // interval.set_missed_tick_behavior(MissedTickBehavior::Burst);
    // 表示如果错过计时间隔，则在下一个可用的计时间隔时执行周期
    // interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
    // 表示如果错过计时间隔，则跳过它并继续执行下一个计时间隔的周期
    // interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
    loop {
        interval.tick().await;
        closure()
    }
}

// pub async fn sleep<T>(secs:u64, closure:T) where T:Fn(){
//     let _ = time::sleep(Duration::from_secs(secs)).await;
//     closure()
// }

pub async fn timeout<T:Fn()>(secs:u64, closure:T){
    let _ = time::timeout(Duration::from_secs(secs), async {
        closure()
    }).await;
}

pub async fn set_timeout_async<T>(secs:u64, closure:T) where T:Future{
    std::thread::sleep(Duration::from_secs(secs));
    closure.await;
}

// pub async fn interval_at<T:Fn()>(start:u64, end:u64, closure:T){
//     let mut interval = time::interval_at(Instant::now() + Duration::from_secs(start), Duration::from_secs(end) );
//     interval.tick().await;
//     closure()
// }